package com.ricky.base.page;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;

/**
 * @Author Ricky
 * @Date 2024/6/20 14:08
 * @Description: 分页封装类
 */
public class PageTool {
    /**
     * 获取基础分页对象从第一页开始排序
     *
     * @return
     * @ page 获取第几页
     * @ size 每页条数 0 为所有条数
     * @ orderFields ["ASC",orderField1],["DESC",orderField2]
     */
    public static Pageable page(Integer page) {
        return page(page, 10);
    }

    public static Pageable page(Integer page, Integer size) {
        page = (page == null || page < 1) ? 1 : page;
        size = (size == null || size <= 0) ? 10 : size;
        Pageable pageable = PageRequest.of(page - 1, size);
        return pageable;
    }

    public static Pageable page(Integer page, SortDto... sorts) {
        return page(page, 10, sorts);
    }

    public static Pageable page(Integer page, Integer size, SortDto... sorts) {
        Sort sort = SortTools.basicSort(sorts);
        page = (page == null || page < 1) ? 1 : page;
        size = (size == null || size <= 0) ? 10 : size;
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return pageable;
    }

    public static Pageable page(Integer page, String... orderFields) {
        return page(page, 10, orderFields);
    }

    public static Pageable page(Integer page, Integer size, String... orderFields) {
        Sort sort = SortTools.basicSort(orderFields);
        page = (page == null || page < 1) ? 1 : page;
        size = (size == null || size <= 0) ? 10 : size;
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return pageable;
    }

    public static Pageable page(Integer page, String[]... orderFields) {
        return page(page, 10, orderFields);
    }

    public static Pageable page(Integer page, Integer size, String[]... orderFields) {
        Sort sort = SortTools.basicSort(orderFields);
        page = (page == null || page < 1) ? 1 : page;
        size = (size == null || size <= 0) ? 10 : size;
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return pageable;
    }

    public static Pageable pageSortList(Integer page, List<SortDto> sorts) {
        return pageSortList(page, 10, sorts);
    }

    public static Pageable pageSortList(Integer page, Integer size, List<SortDto> sorts) {
        Sort sort = SortTools.basicSortSortDto(sorts);
        page = (page == null || page < 1) ? 1 : page;
        size = (size == null || size <= 0) ? 10 : size;
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return pageable;
    }

    public static Pageable page(Integer page, List<String[]> orderFields) {
        return page(page, 10, orderFields);
    }

    public static Pageable page(Integer page, Integer size, List<String[]> orderFields) {
        Sort sort = SortTools.basicSortString(orderFields);
        page = (page == null || page < 1) ? 1 : page;
        size = (size == null || size <= 0) ? 10 : size;
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return pageable;
    }

    public static Pageable pageStringList(Integer page, List<String> orderFields) {
        return pageStringList(page, 10, orderFields);
    }

    public static Pageable pageStringList(Integer page, Integer size, List<String> orderFields) {
        Sort sort = SortTools.basicSort(orderFields);
        page = (page == null || page < 1) ? 1 : page;
        size = (size == null || size <= 0) ? 10 : size;
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        return pageable;
    }

    /**
     * --------------------------------字符串类型----------------------------------------
     */
    public static Pageable page(String page) {
        Integer pageInt = Integer.parseInt(page);
        return page(pageInt);
    }

    public static Pageable page(String page, String size) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        if (size == null || size.equals("")) {
            size = "10";
        }
        Integer pageInt = Integer.parseInt(page);
        Integer sizeInt = Integer.parseInt(size);
        return page(pageInt, sizeInt);
    }

    public static Pageable page(String page, String size, SortDto... sorts) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        if (size == null || size.equals("")) {
            size = "10";
        }
        Integer pageInt = Integer.parseInt(page);
        Integer sizeInt = Integer.parseInt(size);
        return page(pageInt, sizeInt, sorts);
    }

    public static Pageable page(String page, String size, String[]... orderFields) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        if (size == null || size.equals("")) {
            size = "10";
        }
        Integer pageInt = Integer.parseInt(page);
        Integer sizeInt = Integer.parseInt(size);
        return page(pageInt, sizeInt, orderFields);
    }

    public static Pageable page(String page, String[]... orderFields) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        Integer pageInt = Integer.parseInt(page);
        return page(pageInt, orderFields);
    }

    public static Pageable pageSortList(String page, String size, List<SortDto> sorts) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        if (size == null || size.equals("")) {
            size = "10";
        }
        Integer pageInt = Integer.parseInt(page);
        Integer sizeInt = Integer.parseInt(size);
        return pageSortList(pageInt, sizeInt, sorts);
    }

    public static Pageable pageSortList(String page, List<SortDto> sorts) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        Integer pageInt = Integer.parseInt(page);
        return pageSortList(pageInt, 10, sorts);
    }

    public static Pageable page(String page, String size, List<String[]> orderFields) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        if (size == null || size.equals("")) {
            size = "10";
        }
        Integer pageInt = Integer.parseInt(page);
        Integer sizeInt = Integer.parseInt(size);
        return page(pageInt, sizeInt, orderFields);
    }

    public static Pageable page(String page, List<String[]> orderFields) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        Integer pageInt = Integer.parseInt(page);
        return page(pageInt, orderFields);
    }

    public static Pageable pageStringList(String page, List<String> sorts) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        Integer pageInt = Integer.parseInt(page);
        return pageStringList(pageInt, 10, sorts);
    }

    public static Pageable pageStringList(String page, String size, List<String> sorts) {
        if (page == null || page.equals("")) {
            page = "0";
        }
        if (size == null || size.equals("")) {
            size = "10";
        }
        Integer pageInt = Integer.parseInt(page);
        Integer sizeInt = Integer.parseInt(size);
        return pageStringList(pageInt, sizeInt, sorts);
    }


}
